/**
 * 
 */
package fr.isima.tools;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.android.maps.GeoPoint;

import fr.isima.gui.TrackPreferences;
import fr.isima.gui.TrackRecord;

/**
 * @author vincent
 */
public class LocationDetectedBroadcastReceiver extends BroadcastReceiver
{

    private static final String TAG    = "LocationDetectedBroadcastReceiver";
    private KMLGenerator generator = KMLGenerator.getGenerator();


    @Override
    public void onReceive(Context context, Intent intent)
    {

        String action = intent.getAction();
        Log.d(TAG, action);
        if (action.equals(LocationRecorderService.NEW_LOCATION_DETECTED))
        {
            // Convert degree in micro-degree for google maps geopoint

            Double longitude = intent.getDoubleExtra(
                    LocationRecorderService.LONGITUDE, 0) * 1E6;
            Double latitude = intent.getDoubleExtra(
                    LocationRecorderService.LATITUDE, 0) * 1E6;

            Log.d(TAG, "Receiving new Point (" + longitude + ", " + latitude
                    + ")");

            GeoPoint point = new GeoPoint(latitude.intValue(), longitude
                    .intValue());

                generator.addPoint(point);
        }
        else if (action.equals(TrackRecord.STOP))
        {
            String name = intent.getStringExtra(TrackPreferences.PREF_TRACK_NAME);
            Log.d(TAG, "Generating track " + name);
            generator.generate(name);
            generator.removePoinst();
            generator = null;
        }

    }

}
